package com.suncreate.zhtz.iot.ctrl;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.config.KafkaListenerEndpointRegistry;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Api(tags = "智慧台站-kafka",description = "kafka_test")
@RequestMapping(value = "/kafka")
@Controller
public class ProducerController {
    @Autowired
    private KafkaTemplate kafkaTemplate;

    @ApiOperation(value = "kafka生产数据")
    @RequestMapping(value = "/producter",method = RequestMethod.GET)
    public void producter() throws IOException, InterruptedException {
        for (int i = 0; i<=500; i++){
            String[] num = {"1","106","red","white"};
            String[] collector = {"0011","0011","0021","0021"};
            String[] dataItem = {"ch4","ch4","ch2","ch2"};
            Thread.sleep(3000);
            Map<String,Object> map = new HashMap<>();
//            map.put("startTime",new Date().getTime());
            map.put("num",num[i%num.length]);
            map.put("collector",collector[i%collector.length]);
            map.put("dataItem",dataItem[i%dataItem.length]);
            kafkaTemplate.send("topic_vibration",map.toString());
        }
    }

}

